package trlygjj.tanruiliyigenjinjin.web.controller;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import trlygjj.tanruiliyigenjinjin.domain.Salary;
import trlygjj.tanruiliyigenjinjin.page.PageResult;
import trlygjj.tanruiliyigenjinjin.query.SalaryQueryObject;
import trlygjj.tanruiliyigenjinjin.service.ISalaryService;
import trlygjj.tanruiliyigenjinjin.util.JsonResult;
import trlygjj.tanruiliyigenjinjin.util.PermissionName;

import java.util.List;

/**
 * Created by user on 2018/7/11.
 */
@Controller
@RequestMapping("salary")
public class SalaryController {

    @Autowired
    private ISalaryService salaryService;

    /**
     * 提供一个默认不输入后缀,直接可以跳转到具有分页的列表页面
     * @return
     */
    @RequiresPermissions("salary:index")
    @PermissionName("薪资首页")
    @RequestMapping("")
    public String index(){
        return "salary/list";
    }

    /**
     * 具有分页和高级查询
     * @return
     */
    @RequiresPermissions("salary:list")
    @PermissionName("薪资列表")
    @RequestMapping("list")
    @ResponseBody
    public PageResult list(SalaryQueryObject qo){
        PageResult result = salaryService.query(qo);
        return result;
    }

    /**
     * 个人的具有分页和高级查询
     * @return
     */
    @RequiresPermissions("salary:selectByCurrentEmployee")
    @PermissionName("根据当前员工查询薪资(个人管理)")
    @RequestMapping("selectByCurrentEmployee")
    @ResponseBody
    public PageResult selectByCurrentEmployee(SalaryQueryObject qo){
        PageResult result = salaryService.selectByCurrentEmployee(qo);
        return result;
    }


    /**
     * 查询全部
     * @return
     */
    @RequiresPermissions("salary:listAll")
    @PermissionName("工资管理全部数据")
    @RequestMapping("listAll")
    @ResponseBody
    public List<Salary> listAll(){
        return salaryService.selectAll();
    }

    /**
     * 删除
     * @param salaryId 要删除的员工对象id
     * @return 返回JsonResult对象判断是否操作成功
     */
    @RequiresPermissions("salary:delete")
    @PermissionName("工资管理删除")
    @RequestMapping("delete")
    @ResponseBody
    public JsonResult delete(Long salaryId){
        JsonResult result = new JsonResult();
        try{
            salaryService.deleteByPrimaryKey(salaryId);
        }catch (Exception e){
            e.printStackTrace();
            result.mark("操作失败!");
        }
        return result;
    }

    /**
     *更新
     * @param salary 要更新的员工对象
     * @return 返回JsonResult对象判断是否操作成功
     */
    @RequiresPermissions("salary:update")
    @PermissionName("工资管理编辑")
    @RequestMapping("update")
    @ResponseBody
    public JsonResult update(Salary salary){
        JsonResult result = new JsonResult();
        try{
            salaryService.updateByPrimaryKey(salary);
        }catch (Exception e){
            e.printStackTrace();
            result.mark("操作失败!");
        }
        return result;
    }

    /**
     * 保存
     * @param salary 要保存的员工对象
     * @return 返回JsonResult对象判断是否操作成功
     */
    @RequiresPermissions("salary:save")
    @PermissionName("工资管理保存")
    @RequestMapping("save")
    @ResponseBody
    public JsonResult save(Salary salary){
        JsonResult result = new JsonResult();
        try{
            salaryService.insert(salary);
        }catch (Exception e){
            e.printStackTrace();
            result.mark("操作失败!");
        }
        return result;
    }

    @RequiresPermissions("salary:selectByEmployeeId")
    @PermissionName("根据员工查询薪资")
    @RequestMapping("selectByEmployeeId")
    @ResponseBody
    public Salary selectByEmployeeId(Long employeeId){
        return  salaryService.selectByEmployeeId(employeeId);
    }
}
